package 二叉树;

public class No226翻转二叉树 {

    /**
     * 翻转一棵二叉树。
     * <p>
     * 示例：
     * <p>
     * 输入：
     * <p>
     * 4
     * /   \
     * 2     7
     * / \   / \
     * 1   3 6   9
     * 输出：
     * <p>
     * 4
     * /   \
     * 7     2
     * / \   / \
     * 9   6 3   1
     */

    public TreeNode invertTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        TreeNode tempNode = root.right;
        root.right = this.invertTree(root.left);
        root.left = this.invertTree(tempNode);

        return root;
    }

    public static void main(String[] args) {
        TreeNode nodeByArr = TreeNode.getNodeByArr(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8});
        No226翻转二叉树 n = new No226翻转二叉树();
        nodeByArr.printNode();
        TreeNode treeNode = n.invertTree(nodeByArr);
        treeNode.printNode();
    }

}
